package model.wrap.type;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import model.wrap.node.Node;
import models.StringContent;

/**
 * 
 * @author pocteg
 * 
 */
public class StringElement
{
    /**
     * ----PRIVATE-----------ATTRIBUTS-----------INSTANCE----------------------
     */

    /**
     * ----PRIVATE-----------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----PROTECTED---------ATTRIBUTS-----------INSTANCE----------------------
     */
    protected StringContent element;

    /**
     * ----PROTECTED---------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----PUBLIC------------ATTRIBUTS-----------INSTANCE----------------------
     */

    /**
     * ----PUBLIC------------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----CONSTRUCTORS--------------------------------------------------------
     */
    
    /**
     * @author pocteg
     * @param node
     */
    public StringElement(Node node)
    {
	this.element = new StringContent();
	this.element.setId_node(node.getId());
	this.element.setId_structure(node.getStructure().getId());
	this.element.setId_type(node.getType().getID());
	this.element.setContent("");
    }

    /**Return exist StringElement by id.
     * @author pocteg
     * @param id
     */
    public StringElement(Long id)
    {
	try
	{
	    this.element = StringContent.getById(id);
	}
	catch (Exception e)
	{
	}
    }

    /**
     * ----PRIVATE-----------METHOD-----------INSTANCE-------------------------
     */

    /**
     * ----PRIVATE-----------METHOD-----------STATIC---------------------------
     */

    /**
     * ----PROTECTED---------METHOD-----------INSTANCE-------------------------
     */

    /**
     * ----PROTECTED---------METHOD-----------STATIC---------------------------
     */

    /**
     * ----PUBLIC------------METHOD-----------INSTANCE-------------------------
     */
    /**
     * @author pocteg
     * @return
     */
    public Long getId()
    {
	return this.element.getId();
    }

    /**
     * @author pocteg
     */
    public void save()
    {
	this.element.save();
    }
    
    public void update()
    {
	this.element.update();
    }

    /**
     * ----PUBLIC------------METHOD-----------STATIC---------------------------
     */
    /**
     * Create String element with default value.
     * 
     * @param Node
     * @return Return string element.
     */
    public static StringElement createDefaultValueForNode(Node node)
    {
	StringElement element = new StringElement(node);
	element.save();
	return element;
    }
    
    public static Set<Long> searchNodesId(String query)
    {
	List<StringContent> list = StringContent.search(query);
	Set<Long> result = new TreeSet<Long>(); 
	for (StringContent stringContent : list)
	{
	    result.add(stringContent.getId_node());
	}
	return result;
    }

    /**
     * ----GETTERS--&--SETTERS-------------------------------------------------
     */
    
    /**
     * @author pocteg
     * @return
     */
    public Node getNode()
    {
	return new Node(this.element.getId_node());
    }
    
    /**
     * @author pocteg
     * @return
     */
    public String getTitle()
    {
	ArrayList<String> ids = this.getNode().getContentsId();
	ArrayList<String> titles = this.getNode().getContentsTypeTitle();
	
	for (int i=0; i<ids.size(); i++)
	{
	    if(ids.get(i).equals(this.getId().toString()) && this.getNode().getType().getArrayTypes().get(i).equals(Type.STRING))
	    {
		return titles.get(i);
	    }
	}
	
	return "";
    }

    /**
     * @author pocteg
     * @return
     */
    public String getContent()
    {
	return this.element.getContent();
    }
    
    public void setContent(String content)
    {
	this.element.setContent(content);
    }
    
}
